﻿using System;
using Ewk.Runtime.Serialization.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ewk.Runtime.Serialization.UnitTests.PositionBased
{
    /// <summary>
    /// Summary description for PositionBasedDateTimeSerializerTests
    /// </summary>
    [TestClass]
    public class PositionBasedDateTimeSerializerTests
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod, ExpectedException(typeof(DatafieldSerializationException))]
        public void PositionBasedDateTimeSerializer_cannot_deserialize_an_invalid_string_representation_of_a_DateTime()
        {
            var fieldSerializer = new PositionBasedDateTimeSerializer();
            var fieldAttribute = new PositionBasedDatafieldAttribute(0, 1) { DefaultCharacter = '0' };

            fieldSerializer.Deserialize("X", fieldAttribute);
        }

        [TestMethod]
        public void PositionBasedDateTimeSerializer_can_deserialize_an_invalid_string_representation_of_a_DateTime_when_a_client_subscribes_on_the_serializationfailed_event()
        {
            string message = null;
            var fieldSerializer = new PositionBasedDateTimeSerializer();
            var fieldAttribute = new PositionBasedDatafieldAttribute(0, 1) { DefaultCharacter = '0' };

            fieldSerializer.DatafieldSerializationFailed +=
                delegate(object sender, DatafieldSerializationFailedEventArgs e)
                    {
                        message = e.Message;
                    };

            fieldSerializer.Deserialize("X", fieldAttribute);

            Assert.IsInstanceOfType(message, typeof(string));
        }

        [TestMethod]
        public void PositionBasedDateTimeSerializer_can_serialize_a_DateTime_to_a_DateTime_string_with_format_dd_MM_yyyy()
        {
            var fieldSerializer = new PositionBasedDateTimeSerializer();
            var fieldAttribute = new PositionBasedDatafieldAttribute(0, 10) { Format = "dd-MM-yyyy" };
            var date = DateTime.Now;

            for (var i = -365; i < 365; i++)
            {
                var tempDate = date.AddDays(i);
                var result = fieldSerializer.Serialize(tempDate, fieldAttribute);

                Assert.AreEqual(tempDate.ToString(fieldAttribute.Format), result);
            }
        }

        [TestMethod]
        public void PositionBasedDateTimeSerializer_can_deserialize_a_valid_DateTime_string_with_format_dd_MM_yyyy_to_a_DateTime_instance()
        {
            var fieldSerializer = new PositionBasedDateTimeSerializer();
            var fieldAttribute = new PositionBasedDatafieldAttribute(0, 10) { Format = "dd-MM-yyyy" };
            var date = DateTime.Now;

            for (var i = -365; i < 365; i++)
            {
                var tempDate = date.AddDays(i);
                var result = fieldSerializer.Deserialize(tempDate.ToString("dd-MM-yyyy"), fieldAttribute);

                Assert.IsInstanceOfType(result, typeof(DateTime));

                var typedResult = (DateTime)result;
                Assert.AreEqual(tempDate.Date, typedResult.Date);
            }
        }

    }
}
